$(function(){
    function initViewBtn(target) {

        target.find(".btn-view-store").click(function() {
            var href = $(this).data("href");
            $.app.confirm('确认收藏该消息吗？', function () {
                location.href = href;
            }, {title: '收藏消息'});

            return false;
        });

        target.find(".btn-view-recycle").click(function() {
            var href = $(this).data("href");
            $.app.confirm('确认把该消息移动到垃圾箱吗？', function () {
                location.href = href;
            }, {title: '移动消息到垃圾箱'});
            return false;
        });

        target.find(".btn-view-delete").click(function() {
            var href = $(this).data("href");
            $.app.confirm('确认从垃圾箱删除该消息吗？', function () {
                location.href = href;
            }, {title: '删除垃圾箱消息'});
            return false;
        });
    }

    //view.jsp
    $('.message .accordion-toggle').on('click', function () {
        var toggleBtn = $(this);
        var target = $(toggleBtn.attr("href"));
        var id = target.attr("id").replace("collapse", "");
        if(!target.data("loaded")) {
            target.data("loaded", true);
            toggleBtn.append('<img class="loading" src="'+ ctx +'"/static/images/loading.gif" style="height:20px">');
            target.find(".accordion-inner").load(ctx + "/admin/personal/message/" + id + "/content", function() {
                toggleBtn.find(".loading").remove();
            });

            initViewBtn(target);
        }
    });

    initViewBtn($(document));

    // 收藏
    $(".btn-store").click(function () {
        var checkbox = $.table.getAllSelectedCheckbox($(".table"));
        if (checkbox.size() === 0) {
            return;
        }

        $.app.confirm('确认收藏选中的消息吗？', function () {
            location.href = ctx + "/admin/personal/message/batch/store?" + checkbox.serialize();
        }, {title: '收藏消息'});
    });

    // 删除
    $(".btn-recycle-or-delete").click(function () {
        var checkbox = $.table.getAllSelectedCheckbox($(".table"));
        if (checkbox.size() === 0) {
            return;
        }
        
        var panel = $(this).closest(".panel");
        var state = panel.data("state");
        var isRecycle = state !== 'trash_box';
        var stateInfo = panel.data("state-info");

        var title = '删除垃圾箱消息', message = '确认删除垃圾箱选中的消息吗？';
        if (isRecycle) {
            title = '移动消息到垃圾箱';
            message = '确认将' + stateInfo + '中的消息移动到垃圾箱吗？';
        }

        $.app.confirm(message, function () {
            location.href = ctx + "/admin/personal/message/batch/" + (isRecycle ? 'recycle' : 'delete') + "?" + checkbox.serialize();
        }, {title: title});
    });

    // 清空收件箱
    $(".btn-clear").click(function () {

        var panel = $(this).closest(".panel");
        var state = panel.data("state");
        var stateInfo = panel.data("state-info");

        $.app.confirm('确认清空' + stateInfo + '吗？', function () {
            location.href = ctx + "/admin/personal/message/clear/" + state;
        }, {title: '清空' + stateInfo});
    });

    // 标记为已读
    $(".btn-mark-read").click(function() {
        var table = $(".table");
        var checkbox = $.table.getAllSelectedCheckbox(table);
        if (checkbox.size() === 0) {
            return;
        }

        location.href = ctx + "/admin/personal/message/markRead?" + checkbox.serialize() + "&BackURL=" + $.table.encodeTableURL(table);
    });

    $('#grid-table').createGrid({
        url: ctx + '/admin/personal/message/in_box/list',
        height: 250,
        colModel:[
            {label: '标题', name: 'id', width: 80, fixed:true},
            {label: '发件人', name: 'name', width: 60},
            {label: '收件人', name: 'age', width: 90},
            {label: '发送时间', name: 'birthday', width: 150}
        ],
        pager : '#grid-pager'
    });

    $('#sendMessage').on('click', function(e){
        e.preventDefault();
        $.app.openDialog('发送新消息', this.href, {btn: false, area: ['650px', '550px'], maxmin: true})
    })
});